<?php
namespace app\admin\controller;

use app\common\model\Goods as goodsModel;
use app\common\model\Floor as FloorModel;
use app\common\model\Type as TypeModel;
use app\common\controller\AdminBase;
use think\Cache;
use think\Db;

/**
 * 分类管理
 * Class Category
 * @package app\admin\controller
 * author  lixy
 */
class Floor extends AdminBase
{
    protected $floor_model;
    protected $goods_model;
    protected $type_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->floor_model = new FloorModel();
        $this->goods_model  = new GoodsModel();
        $this->type_model  = new TypeModel();
        $category_level_list  = $this->floor_model->getLevelList();
        $this->assign('category_level_list', $category_level_list);
    }

    /**
     * 分类管理
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 添加分类
     * @param string $pid
     * @return mixed
     */
    public function add($pid = '')
    {
        return $this->fetch('add', ['pid' => $pid]);
    }
    /**
     * 添加纸质信息
     */
    public function addpaperinfo($pid = '')
    {
        return $this->fetch('addpaperinfo', ['pid' => $pid]);

    }

    /**
     * 保存分类
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'Category');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['ctime'] = time();
                if ($this->floor_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑分类
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $category = $this->floor_model->find($id);

        return $this->fetch('edit', ['category' => $category]);
    }

    /**编辑纸质信息
     * @param $id
     * @return mixed
     */
    public function editpaperinfo($id)
    {
        $category = $this->floor_model->find($id);

        return $this->fetch('editpaperinfo', ['category' => $category]);
    }

    /**
     * 更新分类
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'Category');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                    if ($this->floor_model->allowField(true)->save($data, $id) !== false) {
                        $this->success('更新成功');
                    } else {
                        $this->error('更新失败');
                    }
            }
        }
    }

    /**
     * 删除分类
     * @param $id
     */
    public function delete($id)
    {
        $pid = $this->floor_model->where(['id' => $id])->value('pid');
        if ($pid == 0) {
            //一级分类
            $status  = $this->floor_model->where(['pid' => $id])->value('id');
            if (!empty($status)) {
                $this->error('此分类下存纸质分类，不可删除');
            }
        } else {
            //纸质 查询该纸下面有没有克数信息
            $res = Db::name('spec')->where('cate2_id',$id)->value('id');
            if (!empty($res)) {
                $this->error('此纸质下有克数，不可删除');
            }
        }
        if ($this->floor_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }


    /**
     * 绑定分类规格
     */

    public function spec(){

        if($this->request->isPost()){

        }else{
            $cate2_id = $this->request->param(['id']);
            $this->assign('cate2_id',$cate2_id);
            return $this->fetch();
        }

    }


    public function upStatus(){
        $param = input();
        $res = Db::name('floor')->where(['id'=>$param['id']])->update(['status'=>$param['status']]);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }


}